package com.yj.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils {
    private static DruidDataSource dataSource;
    private static ThreadLocal<Connection> conns = new ThreadLocal<Connection>();
    private static final String username= "root";
    private static final String password= "o513wangmaoquan";
    private static final String url = "jdbc:mysql://localhost:3306/book?serverTimezone=GMT&useSSL=false&characterEncoding=utf-8&autoReconnect=true";
    private static final String driverClassName= "com.mysql.cj.jdbc.Driver";
    static {
        try {
            Properties properties = new Properties();
            properties.setProperty("url", url);
            properties.setProperty("username", username);
            properties.setProperty("password", password);
            properties.setProperty("driverClassName", driverClassName);
            //创建数据库连接池
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /*
        获取数据库连接池的连接
        @retrun 如果返回null则连接失败
     */
    public static Connection getConnection() {
       Connection conn = conns.get();
       if(conn == null) {
           try {
               conn = dataSource.getConnection();//从数据库池中获取连接
               conns.set(conn);//保存到ThreadLocal对象中，供后面的JDBC操作使用
               conn.setAutoCommit(false);//设置为手动管理事务
           } catch (SQLException throwables) {
               throwables.printStackTrace();
           }
       }
       return conn;
    }

    /**
     * 提交事务并关闭释放连接
     */
    public static void commitAndClose() {
        Connection connection = conns.get();
        if(connection!=null) {
            try {
                connection.commit();//提交事务
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                try {
                    connection.close();//关闭连接释放资源
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        //一定要执行remove操作，否则就会出错，因为tomcat底层使用了线程池技术
        conns.remove();
    }

    /**
     * 回滚事务并关闭释放连接
     */
    public static void rollbackAndClose() {
        Connection connection = conns.get();
        if(connection!=null) {
            try {
                connection.rollback();//回滚事务
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                try {
                    connection.close();//关闭连接释放资源
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        //一定要执行remove操作，否则就会出错，因为tomcat底层使用了线程池技术
        conns.remove();
    }

    /*
        关闭连接，放回数据库连接池

    public static void close(Connection conn) {
        if(conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }*/

    public static void main(String[] args)
    {
        System.out.println(getConnection());
    }
}
